package org.itheima.game.org.tank.game.business

import org.itheima.game.org.tank.game.Config
import org.itheima.game.org.tank.game.enum.Direction
import org.itheima.game.org.tank.game.model.View

/**
 * description ： 移动能力
 * author : chenmengjia
 * email : cmjsp1990@163.com
 * date : 2020/3/14 16:08
 */
interface Movable : View {
    var currentDirection: Direction
    var speed: Int
    //判断移动的物体是否和阻塞物体发生碰撞
    /**
     * return null说明没有碰撞和错误的方向，将要碰撞的方向
     */
    fun willCollision(blockable: Blockable): Direction? {
        //将要碰撞x,y
        var x = this.x
        var y = this.y
        //下一步是否碰撞
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed

        }
        //先和边界检测
        if (x <= 0) return Direction.LEFT
        if (x > Config.gameWidth - width) return Direction.RIGHT
        if (y < 0) return Direction.UP
        if (x > Config.gameHeight - height) return Direction.DOWN

        val checkCollision =
            checkCollision(blockable.x, blockable.y, blockable.width, blockable.height, x, y, width, height)

        return if (checkCollision) currentDirection else null
    }

    fun notifyCollision(direction: Direction?, blockable: Blockable?)

}